package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.exception.ServiceException;
import com.market.common.request.PurchaseRequestParams;
import com.market.common.request.PurchasesRequestParams;
import com.market.common.vo.PurchaseVo;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Commodity;
import com.market.entity.Purchase;
import com.market.mapper.CommodityMapper;
import com.market.mapper.PurchaseMapper;
import com.market.service.PurchaseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/purchase")
public class PurchaseContorller {
    @Resource
    PurchaseService purchaseService;
    @Resource
    CommodityMapper commodityMapper;

    /**
     * 新增
     *
     * @param purchaseRequestParams
     * @return
     * @throws ControllerException
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody PurchaseRequestParams purchaseRequestParams) throws ControllerException {
        /**
         * 获取零售价
         */
        Commodity commodity = commodityMapper.selectRetailPrice(purchaseRequestParams.getCommodityId());
        Purchase purchase = new Purchase();
        purchase.setShopName(purchaseRequestParams.getShopName());
        purchase.setPurchaseEmployeeId(purchaseRequestParams.getPurchaseEmployeeId());
        switch (purchaseRequestParams.getAuditStatus()) {
            case "待审核":
                purchase.setAuditStatus(0);
                break;
            case "审核通过":
                purchase.setAuditStatus(1);
                break;
            case "审核失败":
                purchase.setAuditStatus(2);
                break;
            default:
                throw new ControllerException(ResponseCode.ERROR);
        }
        purchase.setSupplierCommercialTenantName(purchaseRequestParams.getSupplierCommercialTenantName());
        purchase.setCommodityId(purchaseRequestParams.getCommodityId());
        purchase.setPurchaseNumber(purchaseRequestParams.getPurchaseNumber());
        purchase.setPurchaseMoney(commodity.getRetailPrice() * purchase.getPurchaseNumber());
        int i = purchaseService.addPurchase(purchase);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 审核采购单
     *
     * @return
     * @throws ControllerException
     */
    @GetMapping("/find/purchase")
    public ResponseResult<List<PurchaseVo>> findPurchase() throws ControllerException {
        List<PurchaseVo> purchase = purchaseService.findPurchase();
        return ResponseResult.success(ResponseCode.SUCCESS, purchase);
    }

    /**
     * 删除
     *
     * @param purchaseId
     * @return
     */
    @GetMapping("/del")
    public ResponseResult<Integer> del(Long purchaseId) throws ControllerException {
        int i = purchaseService.delPurchase(purchaseId);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 更改
     *
     * @param params
     * @return
     * @throws ControllerException
     */
    @PostMapping("/update")
    public ResponseResult<Integer> update(@RequestBody PurchaseRequestParams params) throws ControllerException {
        Commodity commodity = commodityMapper.selectRetailPrice(params.getCommodityId());
        Purchase purchase = new Purchase();
        purchase.setPurchaseId(params.getPurchaseId());
        purchase.setShopName(params.getShopName());
        switch (params.getAuditStatus()) {
            case "待审核":
                purchase.setAuditStatus(0);
                break;
            case "审核通过":
                purchase.setAuditStatus(1);
                break;
            case "审核失败":
                purchase.setAuditStatus(2);
                break;
            default:
                throw new ControllerException(ResponseCode.ERROR);
        }
        purchase.setPurchaseEmployeeId(params.getPurchaseEmployeeId());
        purchase.setSupplierCommercialTenantName(params.getSupplierCommercialTenantName());
        purchase.setCommodityId(params.getCommodityId());
        purchase.setPurchaseNumber(params.getPurchaseNumber());
        purchase.setPurchaseMoney(commodity.getRetailPrice() * purchase.getPurchaseNumber());
        int i = purchaseService.updatePurchase(purchase);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 查询采购审核通过单
     *
     * @return
     * @throws ControllerException
     */
    @GetMapping("/find/purchase/pass")
    public ResponseResult<List<PurchaseVo>> findPurchasePass() throws ControllerException {
        List<PurchaseVo> purchasePass = purchaseService.findPurchasePass();
        return ResponseResult.success(ResponseCode.SUCCESS, purchasePass);
    }


    /**
     * 搜索
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/find/purchases")
    public ResponseResult<List<PurchaseVo>> findPurchases(@RequestBody PurchasesRequestParams params) throws ControllerException {
        /**
         * 审核状态
         */
        Purchase auditStatus = purchaseService.findAuditStatus(params.getPurchaseId());
        /**
         * 入库状态
         */
        Purchase inStatus = purchaseService.findInStatus(params.getPurchaseId());

        Purchase purchase = new Purchase();
        purchase.setPurchaseId(params.getPurchaseId());
        purchase.setShopName(params.getShopName());
        purchase.setSupplierCommercialTenantName(params.getSupplierCommercialTenantName());
        purchase.setPurchaseEmployeeId(params.getPurchaseEmployeeId());
        List<PurchaseVo> purchases = purchaseService.selectPurchases(purchase);
        for (PurchaseVo purchaseVo : purchases) {
            switch (auditStatus.getAuditStatus()) {
                case 0:
                    purchaseVo.setAuditStatus("待审核");
                    break;
                case 1:
                    purchaseVo.setAuditStatus("审核通过");
                    break;
                case 2:
                    purchaseVo.setAuditStatus("审核失败");
                    break;
                default:
                    break;
            }
            switch (inStatus.getInStatus()) {
                case 0:
                    purchaseVo.setInStatus("待入库");
                    break;
                case 1:
                    purchaseVo.setInStatus("已入库");
                    break;
                case 2:
                    purchaseVo.setInStatus("已作废");
                    break;
                case 3:
                    purchaseVo.setInStatus("部分入库");
                    break;
                default:
                    break;
            }
        }

        return ResponseResult.success(ResponseCode.SUCCESS, purchases);
    }
    /**
     * 查询所有采购单
     *
     * @param params
     * @return
     */
/*    @GetMapping("/find/purchases")
    public ResponseResult<List<PurchaseVo>> findPurchases(@RequestParam PurchasesRequestParams params) {
        Purchase purchase = new Purchase();
        switch (params.getAuditStatus()) {
            case "待审核":
                purchase.setAuditStatus(0);
                break;
            case "审核通过":
                purchase.setAuditStatus(1);
                break;
            case "审核失败":
                purchase.setAuditStatus(2);
                break;
            default:
                throw new ControllerException(ResponseCode.ERROR);
        }
        switch (params.getInStatus()) {
            case "待入库":
                purchase.setInStatus(0);
                break;

            case "已入库":
                purchase.setInStatus(1);
                break;

            case "已作废":
                purchase.setInStatus(2);
                break;

            case "部分入库":
                purchase.setInStatus(3);
                break;
            default:
                throw new ServiceException(ResponseCode.ERROR);
        }
        List<PurchaseVo> purchaseVos = purchaseService.flindPurchases(purchase, params.getPage(), params.getSize());
        return ResponseResult.success(ResponseCode.SUCCESS, purchaseVos);
    }*/
}
